﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WeatherCN_Data.EntityModle;
using System.Net;
using System.Web.Script.Serialization;
using System.IO;
using System.Web;
using System.Text.RegularExpressions;

namespace WeatherCN_Data.DAL
{
    public class WeatherDAL
    {
        public static WeatherInfo_Converted GetDataByCityID(string aCityID)
        {
            WeatherInfo_Converted result = new WeatherInfo_Converted();
            //先从缓存中读取
            result = GetDataByCityIDFromCache(aCityID);
            //如果缓存中没有该纪录，则新建之
            if (result == null)
            {
                result = GetDataByCityIDFromWeatherCN(aCityID);
            }

            return result;
        }

        #region 从应用程序缓存中读取一地的天气预报信息
        private static WeatherInfo_Converted GetDataByCityIDFromCache(string aCityID)
        {
            WeatherInfo_Converted result = new WeatherInfo_Converted();

            if (HttpContext.Current.Cache[aCityID] != null)
            {
                result = HttpContext.Current.Cache[aCityID] as WeatherInfo_Converted;
            }
            else
            {
                result = null;
            }

            return result;
        }

        #endregion


        #region 从中国天气网提取一地的天气预报信息
        private static WeatherInfo_Converted GetDataByCityIDFromWeatherCN(string aCityID)
        {
            WeatherInfo_Converted result = new WeatherInfo_Converted();
            string tpJsonStr = string.Empty;

            WebRequest req = WebRequest.Create("http://m.weather.com.cn/data/" + aCityID + ".html");
            try
            {
                WebResponse WR = req.GetResponse();
                Stream ReceiveStream = WR.GetResponseStream();
                StreamReader readerOfStream = new StreamReader(ReceiveStream, Encoding.UTF8);
                tpJsonStr = readerOfStream.ReadToEnd();

                result = ConvertWeatherInfo(GetInfoFromJson(tpJsonStr));
                //存储或更新缓存
                HttpContext.Current.Cache.Insert(aCityID,result, null, DateTime.Now.AddMinutes(30), System.Web.Caching.Cache.NoSlidingExpiration);
            }
            catch
            {

            }
            return result;
        }
        //Get the WeatherInfo with the OriginalFormat
        private static WeatherInfo GetInfoFromJson(string aJsonStr)
        {
            WeatherInfo result = new WeatherInfo();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            Dictionary<string, object> json = (Dictionary<string, object>)serializer.DeserializeObject(aJsonStr);
            Dictionary<string, object> jsonweatherinfo = json["weatherinfo"] as Dictionary<string, object>;
            object value;
            if (jsonweatherinfo.TryGetValue("city", out value))
                result.city = value.ToString();
            if (jsonweatherinfo.TryGetValue("cityid", out value))
                result.cityid = value.ToString();
            if (jsonweatherinfo.TryGetValue("date_y", out value))
                result.date_y = value.ToString();
            if (jsonweatherinfo.TryGetValue("fchh", out value))
                result.fchh = value.ToString();
            if (jsonweatherinfo.TryGetValue("week", out value))
                result.week = value.ToString();
            if (jsonweatherinfo.TryGetValue("img_title1", out value))
                result.img_title1 = value.ToString();
            if (jsonweatherinfo.TryGetValue("img_title2", out value))
                result.img_title2 = value.ToString();
            if (jsonweatherinfo.TryGetValue("img_title3", out value))
                result.img_title3 = value.ToString();
            if (jsonweatherinfo.TryGetValue("img_title4", out value))
                result.img_title4 = value.ToString();
            if (jsonweatherinfo.TryGetValue("img_title5", out value))
                result.img_title5 = value.ToString();
            if (jsonweatherinfo.TryGetValue("img_title6", out value))
                result.img_title6 = value.ToString();
            if (jsonweatherinfo.TryGetValue("img_title7", out value))
                result.img_title7 = value.ToString();
            if (jsonweatherinfo.TryGetValue("img_title8", out value))
                result.img_title8 = value.ToString();
            if (jsonweatherinfo.TryGetValue("img_title9", out value))
                result.img_title9 = value.ToString();
            if (jsonweatherinfo.TryGetValue("img_title10", out value))
                result.img_title10 = value.ToString();
            if (jsonweatherinfo.TryGetValue("img_title11", out value))
                result.img_title11 = value.ToString();
            if (jsonweatherinfo.TryGetValue("img_title12", out value))
                result.img_title12 = value.ToString();
            if (jsonweatherinfo.TryGetValue("wind1", out value))
                result.wind1 = value.ToString();
            if (jsonweatherinfo.TryGetValue("wind2", out value))
                result.wind2 = value.ToString();
            if (jsonweatherinfo.TryGetValue("wind3", out value))
                result.wind3 = value.ToString();
            if (jsonweatherinfo.TryGetValue("wind4", out value))
                result.wind4 = value.ToString();
            if (jsonweatherinfo.TryGetValue("wind5", out value))
                result.wind5 = value.ToString();
            if (jsonweatherinfo.TryGetValue("wind6", out value))
                result.wind6 = value.ToString();
            if (jsonweatherinfo.TryGetValue("temp1", out value))
                result.temp1 = value.ToString();
            if (jsonweatherinfo.TryGetValue("temp2", out value))
                result.temp2 = value.ToString();
            if (jsonweatherinfo.TryGetValue("temp3", out value))
                result.temp3 = value.ToString();
            if (jsonweatherinfo.TryGetValue("temp4", out value))
                result.temp4 = value.ToString();
            if (jsonweatherinfo.TryGetValue("temp5", out value))
                result.temp5 = value.ToString();
            if (jsonweatherinfo.TryGetValue("temp6", out value))
                result.temp6 = value.ToString();
            if (jsonweatherinfo.TryGetValue("fl1", out value))
                result.fl1 = value.ToString();
            if (jsonweatherinfo.TryGetValue("fl2", out value))
                result.fl2 = value.ToString();
            if (jsonweatherinfo.TryGetValue("fl3", out value))
                result.fl3 = value.ToString();
            if (jsonweatherinfo.TryGetValue("fl4", out value))
                result.fl4 = value.ToString();
            if (jsonweatherinfo.TryGetValue("fl5", out value))
                result.fl5 = value.ToString();
            if (jsonweatherinfo.TryGetValue("fl6", out value))
                result.fl6 = value.ToString();

            return result;
        }
        //Convert WeatherInfo to Well-Format
        private static WeatherInfo_Converted ConvertWeatherInfo(WeatherInfo aWeatherInfo)
        {
            WeatherInfo_Converted result = new WeatherInfo_Converted();

            result.city = aWeatherInfo.city;
            result.cityid = aWeatherInfo.cityid;
            result.date_y = aWeatherInfo.date_y;
            result.fchh = aWeatherInfo.fchh;
            result.week = aWeatherInfo.week;
            result.IsNight = aWeatherInfo.fchh == "18";
            result = ConvertWeatherInfo_ConvertWeather(result, aWeatherInfo);
            result = ConvertWeatherInfo_ConvertTemplate(result, aWeatherInfo);
            result = ConvertWeatherInfo_ConvertWind(result, aWeatherInfo);

            return result;
        }
        //Convert Weather
        private static WeatherInfo_Converted ConvertWeatherInfo_ConvertWeather(WeatherInfo_Converted aWeatherInfo_Converted, WeatherInfo aWeatherInfo)
        {
            if (aWeatherInfo.fchh == "18")
            {
                aWeatherInfo_Converted.img_title11 = string.Empty;

                aWeatherInfo_Converted.img_title12 = aWeatherInfo.img_title1;
                aWeatherInfo_Converted.img_title21 = aWeatherInfo.img_title2;
                aWeatherInfo_Converted.img_title22 = aWeatherInfo.img_title3;
                aWeatherInfo_Converted.img_title31 = aWeatherInfo.img_title4;
                aWeatherInfo_Converted.img_title32 = aWeatherInfo.img_title5;
                aWeatherInfo_Converted.img_title41 = aWeatherInfo.img_title6;
                aWeatherInfo_Converted.img_title42 = aWeatherInfo.img_title7;
                aWeatherInfo_Converted.img_title51 = aWeatherInfo.img_title8;
                aWeatherInfo_Converted.img_title52 = aWeatherInfo.img_title9;
                aWeatherInfo_Converted.img_title61 = aWeatherInfo.img_title10;
                aWeatherInfo_Converted.img_title62 = aWeatherInfo.img_title11;
                aWeatherInfo_Converted.img_title71 = aWeatherInfo.img_title12;
            }
            else
            {
                aWeatherInfo_Converted.img_title11 = aWeatherInfo.img_title1;
                aWeatherInfo_Converted.img_title12 = aWeatherInfo.img_title2;
                aWeatherInfo_Converted.img_title21 = aWeatherInfo.img_title3;
                aWeatherInfo_Converted.img_title22 = aWeatherInfo.img_title4;
                aWeatherInfo_Converted.img_title31 = aWeatherInfo.img_title5;
                aWeatherInfo_Converted.img_title32 = aWeatherInfo.img_title6;
                aWeatherInfo_Converted.img_title41 = aWeatherInfo.img_title7;
                aWeatherInfo_Converted.img_title42 = aWeatherInfo.img_title8;
                aWeatherInfo_Converted.img_title51 = aWeatherInfo.img_title9;
                aWeatherInfo_Converted.img_title52 = aWeatherInfo.img_title10;
                aWeatherInfo_Converted.img_title61 = aWeatherInfo.img_title11;
                aWeatherInfo_Converted.img_title62 = aWeatherInfo.img_title12;

                aWeatherInfo_Converted.img_title71 = string.Empty;
            }
            return aWeatherInfo_Converted;
        }
        //Convert Template
        private static WeatherInfo_Converted ConvertWeatherInfo_ConvertTemplate(WeatherInfo_Converted aWeatherInfo_Converted, WeatherInfo aWeatherInfo)
        {
            if (aWeatherInfo.fchh == "18")
            {
                aWeatherInfo_Converted.temp11 = string.Empty;

                aWeatherInfo_Converted.temp12 = aWeatherInfo.temp1.Split(new char[] { '~' })[0];
                aWeatherInfo_Converted.temp21 = aWeatherInfo.temp1.Split(new char[] { '~' })[1];
                aWeatherInfo_Converted.temp22 = aWeatherInfo.temp2.Split(new char[] { '~' })[0];
                aWeatherInfo_Converted.temp31 = aWeatherInfo.temp2.Split(new char[] { '~' })[1];
                aWeatherInfo_Converted.temp32 = aWeatherInfo.temp3.Split(new char[] { '~' })[0];
                aWeatherInfo_Converted.temp41 = aWeatherInfo.temp3.Split(new char[] { '~' })[1];
                aWeatherInfo_Converted.temp42 = aWeatherInfo.temp4.Split(new char[] { '~' })[0];
                aWeatherInfo_Converted.temp51 = aWeatherInfo.temp4.Split(new char[] { '~' })[1];
                aWeatherInfo_Converted.temp52 = aWeatherInfo.temp5.Split(new char[] { '~' })[0];
                aWeatherInfo_Converted.temp61 = aWeatherInfo.temp5.Split(new char[] { '~' })[1];
                aWeatherInfo_Converted.temp62 = aWeatherInfo.temp6.Split(new char[] { '~' })[0];
                aWeatherInfo_Converted.temp71 = aWeatherInfo.temp6.Split(new char[] { '~' })[1];

            }
            else
            {
                aWeatherInfo_Converted.temp11 = aWeatherInfo.temp1.Split(new char[] { '~' })[0];
                aWeatherInfo_Converted.temp12 = aWeatherInfo.temp1.Split(new char[] { '~' })[1];
                aWeatherInfo_Converted.temp21 = aWeatherInfo.temp2.Split(new char[] { '~' })[0];
                aWeatherInfo_Converted.temp22 = aWeatherInfo.temp2.Split(new char[] { '~' })[1];
                aWeatherInfo_Converted.temp31 = aWeatherInfo.temp3.Split(new char[] { '~' })[0];
                aWeatherInfo_Converted.temp32 = aWeatherInfo.temp3.Split(new char[] { '~' })[1];
                aWeatherInfo_Converted.temp41 = aWeatherInfo.temp4.Split(new char[] { '~' })[0];
                aWeatherInfo_Converted.temp42 = aWeatherInfo.temp4.Split(new char[] { '~' })[1];
                aWeatherInfo_Converted.temp51 = aWeatherInfo.temp5.Split(new char[] { '~' })[0];
                aWeatherInfo_Converted.temp52 = aWeatherInfo.temp5.Split(new char[] { '~' })[1];
                aWeatherInfo_Converted.temp61 = aWeatherInfo.temp6.Split(new char[] { '~' })[0];
                aWeatherInfo_Converted.temp62 = aWeatherInfo.temp6.Split(new char[] { '~' })[1];

                aWeatherInfo_Converted.temp71 = string.Empty;
            }
            return aWeatherInfo_Converted;
        }
        //ConvertWind
        private static WeatherInfo_Converted ConvertWeatherInfo_ConvertWind(WeatherInfo_Converted aWeatherInfo_Converted, WeatherInfo aWeatherInfo)
        {
            if (aWeatherInfo.fchh == "18")
            {
                aWeatherInfo_Converted.wind11 = string.Empty;

                aWeatherInfo_Converted.wind12 = DealWind(aWeatherInfo.wind1, aWeatherInfo.fl1, true);
                aWeatherInfo_Converted.wind21 = DealWind(aWeatherInfo.wind1, aWeatherInfo.fl1, true);
                aWeatherInfo_Converted.wind22 = DealWind(aWeatherInfo.wind2, aWeatherInfo.fl2, true);
                aWeatherInfo_Converted.wind31 = DealWind(aWeatherInfo.wind2, aWeatherInfo.fl2, true);
                aWeatherInfo_Converted.wind32 = DealWind(aWeatherInfo.wind3, aWeatherInfo.fl3, true);
                aWeatherInfo_Converted.wind41 = DealWind(aWeatherInfo.wind3, aWeatherInfo.fl3, true);
                aWeatherInfo_Converted.wind42 = DealWind(aWeatherInfo.wind4, aWeatherInfo.fl4, true);
                aWeatherInfo_Converted.wind51 = DealWind(aWeatherInfo.wind4, aWeatherInfo.fl4, true);
                aWeatherInfo_Converted.wind52 = DealWind(aWeatherInfo.wind5, aWeatherInfo.fl5, true);
                aWeatherInfo_Converted.wind61 = DealWind(aWeatherInfo.wind5, aWeatherInfo.fl5, true);
                aWeatherInfo_Converted.wind62 = DealWind(aWeatherInfo.wind6, aWeatherInfo.fl6, true);
                aWeatherInfo_Converted.wind71 = DealWind(aWeatherInfo.wind6, aWeatherInfo.fl6, true);
            }
            else
            {
                aWeatherInfo_Converted.wind11 = DealWind(aWeatherInfo.wind1, aWeatherInfo.fl1, false);
                aWeatherInfo_Converted.wind12 = DealWind(aWeatherInfo.wind1, aWeatherInfo.fl1, false);
                aWeatherInfo_Converted.wind21 = DealWind(aWeatherInfo.wind2, aWeatherInfo.fl2, false);
                aWeatherInfo_Converted.wind22 = DealWind(aWeatherInfo.wind2, aWeatherInfo.fl2, false);
                aWeatherInfo_Converted.wind31 = DealWind(aWeatherInfo.wind3, aWeatherInfo.fl3, false);
                aWeatherInfo_Converted.wind32 = DealWind(aWeatherInfo.wind3, aWeatherInfo.fl3, false);
                aWeatherInfo_Converted.wind41 = DealWind(aWeatherInfo.wind4, aWeatherInfo.fl4, false);
                aWeatherInfo_Converted.wind42 = DealWind(aWeatherInfo.wind4, aWeatherInfo.fl4, false);
                aWeatherInfo_Converted.wind51 = DealWind(aWeatherInfo.wind5, aWeatherInfo.fl5, false);
                aWeatherInfo_Converted.wind52 = DealWind(aWeatherInfo.wind5, aWeatherInfo.fl5, false);
                aWeatherInfo_Converted.wind61 = DealWind(aWeatherInfo.wind6, aWeatherInfo.fl6, false);
                aWeatherInfo_Converted.wind62 = DealWind(aWeatherInfo.wind6, aWeatherInfo.fl6, false);

                aWeatherInfo_Converted.wind71 = string.Empty;
            }

            return aWeatherInfo_Converted;
        }
        private static string DealWind(string aDirection,string aSpeed,bool aIsFirst)
        {
            string tp1, tp2;
            Regex r = new Regex("(无持续风向|东风|南风|西风|北风|东南风|西南风|西北风|东北风)");
            MatchCollection m = r.Matches(aDirection.Replace("微风","无持续风向"));
            string[] tparrstr = aSpeed.Replace("转", "-").Split(new char[]{'-'});

            if (m.Count == 1)//白天和夜晚是同一个风向
            {
                if (tparrstr.Length == 1)//白天和夜晚是同一个风速
                {
                    tp1 = m[0].Value + tparrstr[0];
                    tp2 = m[0].Value + tparrstr[0];
                }
                else//白天和夜晚不是同一个风速
                {
                    tp1 = m[0].Value + tparrstr[0];
                    tp2 = m[0].Value + tparrstr[1];
                }
            }
            else//白天和夜晚不是同一个风向
            {
                if (tparrstr.Length == 1)//白天和夜晚是同一个风速
                {
                    tp1 = m[0].Value + tparrstr[0];
                    tp2 = m[1].Value + tparrstr[0];
                }
                else//白天和夜晚不是同一个风速
                {
                    tp1 = m[0].Value + tparrstr[0];
                    tp2 = m[1].Value + tparrstr[1];
                }            
            }

            //处理部分风速无“级”的问题
            if (!tp1.EndsWith("级"))
                tp1 += "级";
            if (!tp2.EndsWith("级"))
                tp2 += "级";

            if (aIsFirst)
                return tp1;
            else
                return tp2;
        }

        #endregion
    }
}
